
//开启分享按钮
function onBridgeReady(){
    WeixinJSBridge.call('showOptionMenu');
}
if (typeof WeixinJSBridge == "undefined"){
    if( document.addEventListener ){
        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
    }else if (document.attachEvent){
        document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
    }
}else{
    onBridgeReady();
}
//微信分享信息定义
function _WXShare(obj){
    //初始化参数
    var img,width,height,title,desc,url,appid;
    img=obj.img || 'http://c.weizhi.in/dkt/share.jpg';
    width=obj.width||100;
    height=obj.height||100;
    title=obj.title||document.title;
    desc=obj.desc||document.title;
    url=obj.url||document.location.href;
    appid=obj.appid||'';
    //微信内置方法
    function _ShareFriend() {
        WeixinJSBridge.invoke('sendAppMessage',{
              'appid': appid,
              'img_url': img,
              'img_width': width,
              'img_height': height,
              'link': url,
              'desc': desc,
              'title': title
              }, function(res){
                _report('send_msg', res.err_msg);
          })
    }
    function _ShareTL() {
        WeixinJSBridge.invoke('shareTimeline',{
              'img_url': img,
              'img_width': width,
              'img_height': height,
              'link': url,
              'desc': desc,
              'title': title
              }, function(res) {
              _report('timeline', res.err_msg);
              });
    }
    function _ShareWB() {
        WeixinJSBridge.invoke('shareWeibo',{
              'content': desc,
              'url': url,
              }, function(res) {
              _report('weibo', res.err_msg);
              });
    }
    // 当微信内置浏览器初始化后会触发WeixinJSBridgeReady事件。
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            // 发送给好友
            WeixinJSBridge.on('menu:share:appmessage', function(argv){
                _ShareFriend();
          });

            // 分享到朋友圈
            WeixinJSBridge.on('menu:share:timeline', function(argv){
                _ShareTL();
                });

            // 分享到微博
            WeixinJSBridge.on('menu:share:weibo', function(argv){
                _ShareWB();
           });
    }, false);
}
_WXShare({
  img:'http://c.weizhi.in/dkt/share.jpg',
  title:'分享达人 - 带客通',
  desc:'分享达人 - 带客通',
  url:document.location.href
});
KISSY.use('node,io',function(S,Node,IO){
    var $=Node.all;
    function loadImages(sources, callback){
      var count = 0,
          images ={},
          imgNum = 0;
      for(src in sources){
          imgNum++;
      }
      for(src in sources){
          images[src] = new Image();
          images[src].onload = function(){
              if(++count >= imgNum){
                  callback(images);
              }
          }
          images[src].src = sources[src];
      }
    }
    loadImages(['/wap/img/bg-loader.jpg','/wap/img/ico-logo.png'],function(){
        setTimeout(function(){
            $('.loader').addClass('fadeOut').hide();
            $('.home').addClass('fadeIn');
        },1000);
    });
    var REG={
        name:/^[a-zA-Z0-9\u4e00-\u9fa5]{2,12}$/,
        phone:/(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/,
        wxid:/^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/
    }
    //注册提交
    var submitReg=$('#J_submitReg');
    var name=$('#name');
    var phone=$('#phone');
    var waid = $("#waid").val();
    var uid = $("#uid").val();
    var DATA={}
    DATA.waid = waid;
    DATA.uid = uid;
    submitReg.on('click',function(){
        //姓名
        if(name.length==1){
            var nv=S.trim(name.val());
            if(nv==''){
                alert('姓名不能为空！');
                return false;
            }else if(!REG.name.test(nv)){
                alert('请填写正确的姓名！');
                return false;
            }
            DATA.name=nv;
        }
        //手机
        if(phone.length==1){
            var pv=S.trim(phone.val());
            if(pv==''){
                alert('手机号不能为空！');
                return false;
            }else if(!REG.phone.test(pv)){
                alert('请填写正确的手机号！');
                return false;
            }
            DATA.phone=pv;
        }
        $(this).prop('disabled',true);
        //请求
        IO.post($(this).attr('data-action'),DATA,function(data){
            /*
                status: 状态
                url: 成功后的跳转页面
            */
            if(data.status==200){
                window.location.href=data.url;
            }else{
                $('#J_submitReg').prop('disabled',false);
                alert(data.message);
            }
        },'json');
    });

});